package com.example.teacherservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class TeacherServiceApplication {

    @Autowired
    private RestTemplate restTemplate;

    public static void main(String[] args) {

        SpringApplication.run(TeacherServiceApplication.class, args);
    }

    @Bean
    @LoadBalanced //将会自动在注册中心找服务user-service的url地址，然后选择一个负载均衡出来，做调用。
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    /**
     * 远程调用user-service
     *
     * @return
     */
    @GetMapping("/info")
    public String info() {
        String forObject = restTemplate.getForObject("http://USER-SERVICE/info", String.class);
        return "来自远程调用的结果：" + forObject;

    }

}
